package com.amoby.main.action.business;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amoby.base.common.BaseAction;
import com.amoby.base.utils.JacksonBuilder;
import com.amoby.main.entity.Area;
import com.amoby.main.service.AreaService;
import com.fasterxml.jackson.core.JsonProcessingException;
/**
 * ParamAction Desc：区域管理
 * @address http://www.yingmob.com/
 * @author zhaoyy
 * @date 2016年4月11日 上午10:44:37
 */
@Controller
public class BusinessAreaAction extends BaseAction {
    @Autowired
    private AreaService areaService;

    /**
     * 初始化页面
     * @author Lasing 2016-5-24 上午11:33:52
     * @param model
     * @return
     */
    @RequestMapping(value = "/menus/area_mgr.html", method = RequestMethod.GET)
    public String index(ModelMap model) {
        List<Map<String, Object>> list = areaService.getAllAreas();
        for (Map<String, Object> map : list) {
            map.put("status", 1);
        }
        model.put("areas", list);
        try {
            model.put("areasJson", JacksonBuilder.mapper().writeValueAsString(model.get("areas")));
        } catch (JsonProcessingException e) {
            getLog(this).error(e.getMessage(), e);
        }
        return AD_HTML + "business/area_mgr";
    }
    
    /**
     * 根据parentid查询
     * @author Lasing 2016-6-6 上午11:32:20
     * @param parentId
     * @param request
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/shop_city.html")
    public Object parentSub(Long parentId, HttpServletRequest request, ModelMap model) {
        Map<String, Object> result = getResultMap();
        List<Map<String, Object>> area = areaService.queryByParentId(parentId);
        result.put("area", area);
        result.put(RESULT, true);
        result.put(MESSAGE, "查询成功！");
        return result;
    }

    /**
     * 新增区域
     * @author Lasing 2016-5-24 上午11:34:05
     * @param area
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/area/add.html", method = RequestMethod.POST)
    public Object addArea(Area area) {
        Map<String, Object> result = getResultMap();
        try {
            if (areaService.insertArea(area) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "新增成功！");
                result.put("id", area.getId());
                result.put("code", area.getCode());
            } else {
                result.put(RESULT, false);
                result.put(MESSAGE, "新增失败！");
            }
        } catch (Exception e) {
            result.put(RESULT, false);
            result.put(MESSAGE, "系统异常处理失败！");
            getLog(this).error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 修改区域
     * @author Lasing 2016-5-24 上午11:34:17
     * @param area
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/area/upd.html", method = RequestMethod.POST)
    public Object updArea(Area area) {
        Map<String, Object> result = getResultMap();
        try {
            if (area.getId() != null && areaService.updateArea(area) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "更新成功！");
                result.put("code", area.getCode());
            } else {
                result.put(RESULT, false);
                result.put(MESSAGE, "更新失败！");
            }
        } catch (Exception e) {
            result.put(RESULT, false);
            result.put(MESSAGE, "系统异常处理失败！");
            getLog(this).error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 删除区域
     * @author Lasing 2016-5-24 上午11:34:27
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/area/del.html", method = RequestMethod.POST)
    public Object delArea(Integer id) {
        Map<String, Object> result = getResultMap();
        try {
            if (id != null && areaService.deleteArea(id) > 0) {
                result.put(RESULT, true);
                result.put(MESSAGE, "删除成功！");
            } else {
                result.put(RESULT, false);
                result.put(MESSAGE, "删除失败！");
            }
        } catch (Exception e) {
            result.put(RESULT, false);
            result.put(MESSAGE, "系统异常处理失败！");
            getLog(this).error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 验证区域唯一性
     * @author Lasing 2016-5-24 上午11:34:40
     * @param code
     * @param parentId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/area/checkDel.html", method = RequestMethod.POST)
    public Object checkDelArea(String code, Long parentId) {
        Map<String, Object> result = getResultMap();
        try {
            if (code != null && !"".equals(code) && areaService.checkDelArea(code, parentId) > 0) {
                result.put(RESULT, false);
                result.put(MESSAGE, "该地区已在商家中使用,不能删除！");
            } else {
                result.put(RESULT, true);
                result.put(MESSAGE, "验证成功！");
            }
        } catch (Exception e) {
            result.put(RESULT, false);
            result.put(MESSAGE, "系统异常处理失败！");
            getLog(this).error(e.getMessage(), e);
        }
        return result;
    }
}
